using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DARemitoN
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DARemitoNInsert
        /// <summary>
        /// Inserta RemitoN 
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitoN"></param>
        /// <returns>RemitoN</returns>
        public void DARemitoNInsert(string Connection, RemitoN remito)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);

            DbCommand cmd = DataBase.GetStoredProcCommand("RemitoN_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, remito.Sucursal);
                    DataBase.AddInParameter(cmd, "@Numero", DbType.Int32, remito.Numero);
                    DataBase.AddInParameter(cmd, "@Prefijo", DbType.String, remito.Prefijo);
                    DataBase.AddInParameter(cmd, "@Talonario", DbType.Int16, remito.Talonario);
                    DataBase.AddInParameter(cmd, "@Nombre", DbType.String, remito.Nombre);
                    DataBase.AddInParameter(cmd, "@Id_Remito", DbType.Int64, remito.Id_Remito);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DARemitoNGetOne
        /// <summary>
        /// Devuelve una ReminoN  ( Numero de Remito)
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitoN"></param>
        /// <returns>RemitoN</returns>
        public RemitoN DARemitoNGetOne(string Connection, RemitoN remito)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("RemitoN_GetOne");

            List<RemitoN> listRemitoN = new List<RemitoN>();
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Numero", DbType.Int16, remito.Numero);
                    //DataBase.AddInParameter(cmd, "OrdenDeRegistros", DbType.String, OrdenDeRegistros);
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        RemitoN oRemito = new RemitoN();
                        oRemito.Numero = int.Parse(Odbreader["Numero"].ToString());

                        oRemito.Id_Remito = Int32.Parse(Odbreader["Id_Remito"].ToString());

                        listRemitoN.Add(oRemito);
                    }
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
            if (listRemitoN.Count > 0)
            {
                return listRemitoN[0];
            }
            else
            {
                RemitoN remitoRet = new RemitoN();
                return remitoRet;
            }
        }
        #endregion

        #region DARemitoProximoNumero
        /// <summary>
        /// Devuelve proximo numero de Factura
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="RemitoN"></param>
        /// <returns>int</returns>
        public int DARemitoProximoNumero(string Connection, RemitoN remito)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("ProximoNumero");
            int iProximo = 0;
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Tabla", DbType.String, "RemitoN");
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.String, remito.Sucursal.ToString());
                    DataBase.AddInParameter(cmd, "@Campo", DbType.String, "Talonario");
                    DataBase.AddInParameter(cmd, "@Valor", DbType.String, remito.Talonario.ToString());

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        iProximo = Convert.ToInt16(Odbreader["ProxNumero"].ToString());
                    }
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
            return iProximo;
        }
        #endregion
    }
}

